﻿
using Castle.Core.Internal;
using Coldairarrow.Business.Interface;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.Interface;
using Coldairarrow.Util;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.VisualBasic.FileIO;
using Newtonsoft.Json.Linq;
using org.apache.zookeeper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;

namespace Coldairarrow.Business.FileBusiness
{
    public class FileTestInterfaceBusiness : Flie_BaseBusiness, IJob_Business, ITransientDependency
    {

        public Base_InterfaceOption fileOption { get; set; }
        public FileTestInterfaceBusiness()
        {

            this.InterfaceName = "ONE_LINK文件备份";
            //数据库连接
            ConString = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            SqlDb = (SqlServerHelper)DbHelperFactory.GetDbHelper(EFCore.Sharding.DatabaseType.SqlServer, ConString);
            this.interfaceOption = SqlDb.GetListBySql<Base_InterfaceOption>(@$"SELECT  * FROM  Base_InterfaceOption(NOLOCK) WHERE Name='{InterfaceName}' ");
            this.fileOption = this.interfaceOption.Where(o => o.OperateType == "FileBackup").First();//API配置

            BeforeBackupFileEvent += FileTestInterfaceBusiness_BeforeBackupFileEvent;
            AfterBackupFileEvent += FileTestInterfaceBusiness_AfterBackupFileEvent;
            OnBackupFileEvent += FileTestInterfaceBusiness_OnBackupFileEvent;

        }

        private void FileTestInterfaceBusiness_OnBackupFileEvent(object sender, BackupFileEventArgs BackupFileEventArgs)
        {

        }

        private void FileTestInterfaceBusiness_AfterBackupFileEvent(object sender, BackupFileEventArgs BackupFileEventArgs)
        {

        }

        private void FileTestInterfaceBusiness_BeforeBackupFileEvent(object sender, BackupFileEventArgs BackupFileEventArgs)
        {
            //判断路径，文件是否存在
            if (!Directory.Exists(BackupFileEventArgs.Path))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(BackupFileEventArgs.Path);
                directoryInfo.Create();
            }
            //判断文件是否存在
            if (!File.Exists(BackupFileEventArgs.FullPath))
            {
                BackupFileEventArgs.IsExist = true;
            }
        }



        /// <summary>
        /// 新增更新表
        /// </summary>
        public async void ExecuteInterface()
        {

            try
            {

                SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
                //获取所有需要备份文件的接口信息
                List<Base_InterfaceOptionEntry> interfaceEntry = SqlDb.GetListBySql<Base_InterfaceOptionEntry>(@$"SELECT  * FROM  Base_InterfaceOptionEntry(NOLOCK) WHERE OptionId='{fileOption.Id}' ");
                //所有文件列表
                List<AttaInfo> attaInfos = new List<AttaInfo>();
                foreach (var item1 in interfaceEntry)
                {
                    if (item1.IsEnable == "Y")
                    {
                        Type type = Type.GetType(item1.TargetField);
                        if (type == null) throw new Exception("找不到任务，请检查任务名称是否错误");
                        if (type.IsSubclassOf(typeof(IAttachment_Business))) throw new Exception("此任务未继承IJob_Business基类，请检查");
                        // 使用反射创建类的实例
                        IAttachment_Business instance = (IAttachment_Business)Activator.CreateInstance(type);
                        List<AttaInfo> temp = new List<AttaInfo>();
                        //执行获取文件信息
                        temp = await instance.GetAttachmentList();
                        attaInfos.AddRange(temp);
                    }
                }

                //执行文件备份
                if (attaInfos.Count > 0)
                {
                    await this.ExecuteBackupFile(attaInfos, fileOption, SqlDb);
                    string ww = "";
                }
               
            }
            catch (Exception ex)
            {
                AddLog(SqlDb, ex.Message, InterfaceName, "erro", "");
            }
        }


        /// <summary>
        /// 任务
        /// </summary>
        public void Job()
        {
            ExecuteInterface();
        }
    }



}
